package com.mcxx.modules.miniSalvation.fundpay.service.write;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.miniSalvation.fundpay.dao.write.FundRecoverWriteDao;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundRecoverEntity;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.LocalDate;

@Service
@TaTransactional
public class FundRecoverWriteService {

    @Autowired
    private FundRecoverWriteDao fundRecoverWriteDao;



    public int insert(FundRecoverEntity fundRecover) {
    	return fundRecoverWriteDao.insert(fundRecover);
    }


    public int insertIgnoreNull(FundRecoverEntity fundRecover) {
    	return fundRecoverWriteDao.insertIgnoreNull(fundRecover);
    }


    public int update(FundRecoverEntity fundRecover) {
    	return fundRecoverWriteDao.update(fundRecover);
    }


    public int updateIgnoreNull(FundRecoverEntity fundRecover) {
    	return fundRecoverWriteDao.updateIgnoreNull(fundRecover);
    }


    public int delete(FundRecoverEntity fundRecover) {
    	return fundRecoverWriteDao.delete(fundRecover);
    }


    public void addSalaryRecovery(FundRecoverEntity param){
        param.setState(Constant.RecoveryState.UNCONFIRM);
        param.setId(Constant.getUUID());
        param.setCreateTime(Date.valueOf(LocalDate.now()));
        int rst = fundRecoverWriteDao.insertIgnoreNull(param);
        if(rst <= 0){
            throw new BizException(FailureCode.ERR_9999);
        }
    }


    public int modifySalaryRecovery(FundRecoverEntity param) {
        return fundRecoverWriteDao.updateIgnoreNull(param);
    }


    public int cancelSalaryRecovery(String ids) {
        return fundRecoverWriteDao.cancelSalaryRecovery(ids.split(","));
    }


    public int confirmSalaryRecovery(String ids) {
        return fundRecoverWriteDao.confirmSalaryRecovery(ids.split(","));
    }


    public int delSalaryRecovery(String ids) {
        String[]idArray=getIdArray(ids);
        int num=fundRecoverWriteDao.delSalaryRecovery(getIdArray(ids));
        if(num!=idArray.length){
           return 0;
        }
        return num;
    }


    private String[] getIdArray(String ids) {
        if(ids.endsWith(",")) ids = ids.substring(0, ids.lastIndexOf(","));
        return ids.split(",");
    }
}
